package com.aletto.general_store.ui.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.aletto.general_store.R;
import com.aletto.general_store.data.model.ConsumerOrder;
import com.aletto.general_store.data.model.OrderItem;
import com.aletto.general_store.presenters.ConsumerOrderConfirmTask;
import com.aletto.general_store.presenters.OrderCancelArbitrationTask;
import com.aletto.general_store.ui.activities.OrderCommentActivity;
import com.aletto.general_store.ui.activities.orders.OrderConsumerDustbinActivity;
import com.aletto.general_store.ui.activities.orders.OrderConsumerDeliveryActivity;
import com.aletto.general_store.ui.activities.orders.OrderConsumerRefundActivity;
import com.aletto.general_store.ui.popup.OrderRefundPopup;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;

import java.math.BigDecimal;
import java.util.List;

public class ConsumerOrderAdapter extends BaseMultiItemQuickAdapter<ConsumerOrder, BaseViewHolder> {

    public ConsumerOrderAdapter() {
        addItemType(ConsumerOrder.delivery, R.layout.item_consumer_order_delivery); // 待发货
        addItemType(ConsumerOrder.dustbin, R.layout.item_consumer_order_trade_dustbin); // 待收货
        addItemType(ConsumerOrder.comment, R.layout.item_consumer_order_comment); // 待评分
        addItemType(ConsumerOrder.refund, R.layout.item_consumer_order_refund); // 退款

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            ConsumerOrder item = (ConsumerOrder) adapter.getItem(position);

            switch (item.getItemType()) {
                case ConsumerOrder.delivery: // 待发货
                    context.startActivity(OrderConsumerDeliveryActivity.newIntent(context, item));
                    break;

                case ConsumerOrder.dustbin: // 待收货
                    context.startActivity(OrderConsumerDustbinActivity.newIntent(context, item));
                    break;

                case ConsumerOrder.comment:
                    context.startActivity(OrderCommentActivity.newIntent(context, item));
                    break;

                case ConsumerOrder.refund:
                    context.startActivity(OrderConsumerRefundActivity.newIntent(context, item));
                    break;
            }
        });
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ConsumerOrder item) {
        Context context = getContext();
        Resources resources = context.getResources();

        OrderItem.ShopDTO shop = item.getShop();

        BigDecimal price = new BigDecimal(item.getPrice()).multiply(new BigDecimal(item.getNum()));

        holder.setText(R.id.tv_goods_title, item.getGoods_name())
                .setText(R.id.tv_order_status, getStatusString(item.getStatus())) // 状态
                .setText(R.id.tv_sold_number, "× " + item.getNum())
                .setText(R.id.tv_goods_price, "实付：" + XHelper.toPrice(price) + " USDT");

        if (shop != null) {
            ImageView iv_shop_logo = holder.getView(R.id.iv_shop_logo);
            holder.setText(R.id.tv_shop_name, shop.getName());
            GlideApp.with(iv_shop_logo).load(shop.getHead_img()).avatar().into(iv_shop_logo);
        }

        // 商品图片
        List<String> goodsImg = item.getGoods_img();
        if (goodsImg != null && !goodsImg.isEmpty()) {
            ImageView iv_goods_cover = holder.getView(R.id.iv_goods_cover);
            GlideApp.with(iv_goods_cover).load(goodsImg.get(0)).normal().into(iv_goods_cover);
        }

        switch (item.getItemType()) {
            case ConsumerOrder.delivery:
                holder.getView(R.id.tv_store_delivery).setOnClickListener(v -> // 申请退款
                        new XPopup.Builder(v.getContext()).asCustom(new OrderRefundPopup(v.getContext(), item)).show());

                holder.setText(R.id.tv_store_delivery, item.getBtc_order_id() == 0 ? "区块确认中" : resources.getString(R.string.order_refund));
                break;

            case ConsumerOrder.dustbin:
                holder.getView(R.id.tv_logistics).setOnClickListener(v -> // 申请退款
                        new XPopup.Builder(v.getContext()).asCustom(new OrderRefundPopup(v.getContext(), item)).show());

//                holder.getView(R.id.tv_confirm).setOnClickListener(v -> // 确认收货
//                        new ConsumerOrderConfirmTask(item.getOrderId(), s -> remove(item)).execute());
                break;

            case ConsumerOrder.comment:

                break;

            case ConsumerOrder.refund:

                View tv_logistics = holder.getView(R.id.tv_logistics);

                if (item.getStatus() == 5){ // 申请退货

                    holder.setText(R.id.tv_store_customer, R.string.order_service)
                                    .setText(R.id.tv_store_delivery, R.string.order_customer_service)
                                    .setText(R.id.tv_logistics, R.string.order_cancel_refund);

                    tv_logistics.setOnClickListener(v -> // 取消申请
                            new OrderCancelArbitrationTask(item.getBtc_order_id()).execute());

                } else {
                    holder.setVisible(R.id.tv_store_customer, false)
                            .setText(R.id.tv_store_delivery, R.string.order_service)
                            .setText(R.id.tv_logistics, R.string.order_customer_service);

                    tv_logistics.setOnClickListener(v->{});
                }

                break;
        }
    }

    private String getStatusString(int status) {
        // 1下单，2支付成功，3已发货，4途中，5申请退货，6退货中 7退货成功 8已签收 9已完成 10拒绝退款
        switch (status) {
            case 1:
                return "下单";
            case 2:
                return "支付成功";

            case 3:
            case 4: // 运输中
                return "已发货";

            case 5:
                return "已申请退款";
            case 6:
                return "退货中";
            case 7:
                return "退款成功";
            case 8:
                return "已签收";
            case 9:
                return "已完成";
            case 10:
                return "退款失败";
        }

        return "未知状态";
    }

}
